package com.katze.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.katze.boot.system.dao.DictMapper;
import com.katze.boot.system.domain.SysDict;
import com.katze.boot.system.service.DictService;
import com.katze.common.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

@Service("dictService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements DictService {

    @PostConstruct
    public void loadDict() {
        List<SysDict> dicts = this.baseMapper.selectList(Wrappers.lambdaQuery(SysDict.class));
        if (dicts != null) {
//            Map<String, List<SysDict>> groups = dicts.stream().collect(Collectors.groupingBy(SysDict::getFieldName));
//            Map<String, Map<String, String>> data = new LinkedHashMap<>();
//            groups.forEach((key, value) -> {
//                Map<String, String> map = value.stream().collect(Collectors.toMap(SysDict::getKey, SysDict::getValue));
//                data.put(key, map);
//            });
//            System.getProperties().put("Dictionaries", data);
        }
    }

    @Override
    public Object findDicts(Pagination request, SysDict dict) {
        try {
            LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDict::getTableName, dict.getTableName());

            if (StringUtils.isNotBlank(dict.getValue())) {
                queryWrapper.eq(SysDict::getValue, dict.getValue());
            }
            if (StringUtils.isNotBlank(dict.getFieldName())) {
                queryWrapper.eq(SysDict::getFieldName, dict.getFieldName());
            }
            queryWrapper.orderByAsc(SysDict::getOrder).orderByAsc(SysDict::getDictId);
            if (request.isEnabled()) {
                return this.page(request.toPage(), queryWrapper);
            } else {
                return this.baseMapper.selectList(queryWrapper);
            }
        } catch (Exception e) {
            log.error("获取字典信息失败", e);
            return null;
        }
    }

    @Override
    public List<SysDict> findDicts() {
        return this.baseMapper.findDicts();
    }

    @Override
    @Transactional
    public void createDict(SysDict dict) {
        this.save(dict);
    }

    @Override
    @Transactional
    public void updateDict(SysDict dict) {
        this.baseMapper.updateById(dict);
    }

    @Override
    @Transactional
    public void updateByCombinationId(SysDict dict) {
        this.baseMapper.updateByCombinationId(dict);
    }

    @Override
    @Transactional
    public void deleteDicts(String[] dictIds) {
        List<String> list = Arrays.asList(dictIds);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    @Transactional
    public void deleteByCombinationId(String combinationId) {
        this.baseMapper.deleteByCombinationId(combinationId);
    }
}